package com.kgc.scd.service.impl;

import com.kgc.scd.bean.ArrivalUser;
import com.kgc.scd.bean.NotifyProArrive;
import com.kgc.scd.bean.NotifyProArriveExample;
import com.kgc.scd.bean.Product;
import com.kgc.scd.mapper.NotifyProArriveMapper;
import com.kgc.scd.mapper.ProductMapper;
import com.kgc.scd.service.NotifyProArriveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created On : 2022/6/16.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 到货提醒业务接口实现类
 */
@Slf4j
@Service
public class NotifyProArriveServiceImpl implements NotifyProArriveService {

    @Autowired(required = false)
    private NotifyProArriveMapper notifyProArriveMapper;

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Override
    public boolean addArrivalRemind(NotifyProArrive notifyProArrive) {
        log.info("更新到货提醒表：{}", notifyProArrive);
        return notifyProArriveMapper.insertSelective(notifyProArrive) > 0;
    }

    @Override
    public boolean checkReminded(String userId, String proId) {
        NotifyProArriveExample notifyProArriveExample = new NotifyProArriveExample();
        NotifyProArriveExample.Criteria criteria = notifyProArriveExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andProIdEqualTo(proId).andStatusEqualTo(0);
        return notifyProArriveMapper.selectByExample(notifyProArriveExample).isEmpty() ?
                false :
                true;
    }

    @Override
    public List<Product> queryUserRemindProduct(String userId) {
        List<NotifyProArrive> notifyProArrives = queryNotifyProArriveByUid(userId);
        List<Product> products = new ArrayList<>();

        for (NotifyProArrive n : notifyProArrives) {
            products.add(productMapper.selectByPrimaryKey(n.getProId()));
        }

        return products;
    }

    @Override
    public List<NotifyProArrive> queryNotifyProArriveByUid(String userId) {
        NotifyProArriveExample notifyProArriveExample = new NotifyProArriveExample();
        NotifyProArriveExample.Criteria criteria = notifyProArriveExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andStatusEqualTo(0);
        notifyProArriveExample.setOrderByClause(" id desc ");
        return notifyProArriveMapper.selectByExample(notifyProArriveExample);
    }


    @Override
    public List<ArrivalUser> queryArrivalUserByProId(String proId) {
        log.info("开始查询订阅商品：{}到货的用户信息");

        return notifyProArriveMapper.selectArrivalUser(proId);
    }

    @Override
    public boolean modNotifyProArriveById(NotifyProArrive notifyProArrive) {
        return notifyProArriveMapper.updateByPrimaryKeySelective(notifyProArrive) > 0;
    }


}
